package cloud.shyidong.core.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/3/27
 */
public final class DateUtils {
    
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    /**
     * 时间戳转化为字符串
     *
     * @param l
     * @return
     */
    public static String long2Str(long l) {
        try {
            return DateUtils.date2Str(new Date(l));
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String date2Str(Date date) {
        return date2Str(date, null);
    }
    
    public static String date2Str(Date date, String format) {
        if (StringUtils.isEmpty(format)) {
            format = YYYY_MM_DD_HH_MM_SS;
        }
        try {
            return new SimpleDateFormat(format).format(date);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 字符串转化为日期
     *
     * @param s
     * @return
     */
    public static Date str2Date(String s) {
        return str2Date(s, YYYY_MM_DD);
    }
    
    /**
     * 字符串转化为日期
     *
     * @param s
     * @param format
     * @return
     */
    public static Date str2Date(String s, String format) {
        try {
            return new SimpleDateFormat(format).parse(s);
        } catch (Exception e) {
            return null;
        }
    }
    
    public static Date getNow() {
        return new Date();
    }
    
    public static Timestamp getNowTimestamp() {
        return new Timestamp(new Date().getTime());
    }
    
    public static Timestamp str2Timestamp(String s) {
        return str2Timestamp(s, YYYY_MM_DD);
    }
    
    public static Timestamp str2Timestamp(String s, String format) {
        try {
            return new Timestamp(new SimpleDateFormat(format).parse(s).getTime());
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * localDateTime转字符串
     *
     * @param localDateTime
     * @return
     */
    public static String localDateTime2Str(LocalDateTime localDateTime) {
        return localDateTime2Str(localDateTime, DateUtils.YYYY_MM_DD);
    }
    
    /**
     * localDateTime转字符串
     *
     * @param localDateTime
     * @param pattern
     * @return
     */
    public static String localDateTime2Str(LocalDateTime localDateTime, String pattern) {
        try {
            return DateTimeFormatter.ofPattern(pattern).format(localDateTime);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 获取挡墙时间的一年后时间
     *
     * @return
     */
    public static Date getAfterYear() {
        return getAfterYear(getNow());
    }
    
    /**
     * 获取指定时间的一年后时间
     *
     * @param date
     * @return
     */
    public static Date getAfterYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        return calendar.getTime();
    }
    
    public static Date getAfterDays(Integer days) {
        return getAfterDate(days, null, null);
    }
    
    public static Date getAfterHours(Integer hours) {
        return getAfterDate(null, hours, null);
    }
    
    public static Date getAfterMinutes(Integer minutes) {
        return getAfterDate(null, null, minutes);
    }
    
    public static Date getAfterDayAndHours(Integer days, Integer hours) {
        return getAfterDate(days, hours, null);
    }
    
    public static Date getAfterHourAndMinute(Integer hours, Integer minutes) {
        return getAfterDate(null, hours, minutes);
    }
    
    public static Date getAfterDate(Integer days, Integer hours, Integer minutes) {
        return getAfterDate(getNow(), days, hours, minutes);
    }
    
    public static Date getAfterDate(Date date, Integer days, Integer hours, Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (ObjectUtils.isNotNull(days)) {
            calendar.add(Calendar.DAY_OF_MONTH, days);
        }
        if (ObjectUtils.isNotNull(hours)) {
            calendar.add(Calendar.HOUR, hours);
        }
        if (ObjectUtils.isNotNull(minutes)) {
            calendar.add(Calendar.MINUTE, minutes);
        }
        return calendar.getTime();
    }
    
}
